//
//  AvatarBrowser.m
//  CenturyInterconnection
//
//  Created by 郭先生 on 16/4/18.
//  Copyright © 2016年 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "AvatarBrowser.h"

static CGRect oldframe;
@implementation AvatarBrowser


+(void)showImage:(UIImageView *)avatarImageView{
    UIImage *image=avatarImageView.image;
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    oldframe=[avatarImageView convertRect:avatarImageView.bounds toView:window];
    backgroundView.backgroundColor=[UIColor colorWithRed:0.25 green:0.25 blue:0.25 alpha:0.5];
    
    backgroundView.alpha=0.5;
    
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:oldframe];
    imageView.image=image;
    imageView.tag=1;
    imageView.userInteractionEnabled=YES;
    [backgroundView addSubview:imageView];
    [window addSubview:backgroundView];
    
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    [backgroundView addGestureRecognizer:tap];
    

    
    
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame=CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
        backgroundView.alpha=1;
    } completion:^(BOOL finished) {
    }];
    UIPinchGestureRecognizer*pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchView:)];
    [imageView addGestureRecognizer:pinch];
}

+(void)hideImage:(UITapGestureRecognizer*)tap{
    UIView *backgroundView=tap.view;
    UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame=oldframe;
        backgroundView.alpha=0.5;
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
    }];
}


+(void)pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
    NSLog(@"图片缩小");
//    UIView *view = pinchGestureRecognizer.view;
    if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan ||pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        pinchGestureRecognizer.view.transform = CGAffineTransformMakeScale(  pinchGestureRecognizer.scale,pinchGestureRecognizer.scale);
        pinchGestureRecognizer.scale=1.0;
    }
    
}


@end
